{subtemplate common/header}
<link rel="stylesheet" type="text/css" href="data/cache/style_{STYLEID}_forum_post.css?{VERHASH}">
<style>
.sinf {
	float: left;
	padding-right: 14px;
	width: 500px;
	border-right: 1px dashed #CCC;
}
</style>
<script src="static/js/calendar.js?{VERHASH}" type="text/javascript"></script>
<div id="pt" class="bm cl">
	<div class="z">
		<a href="./" class="nvhm" title="{lang homepage}">$_G[setting][bbname]</a> <em>&rsaquo;</em>
		<a href="plugin.php?id=xunku_editor_spider:index&action=later&fid={$fid}">{$_G['setting']['navs'][6]['navname']}</a> <em>&rsaquo;</em>
		<a href="plugin.php?id=xunku_editor_spider:index&action=later&fid={$fid}">{lang xunku_editor_spider:editor_nav_title}</a> <em>&rsaquo;</em>
		{lang xunku_editor_spider:editor_task_list_nav}
	</div>
</div>

    <table cellspacing="0" cellpadding="0" class="dt valt">
	<tbody id="joblist">
		<tr>
			<th class="xw1">{lang xunku_editor_spider:editor_task_list_num}</th>
			<th class="xw1">{lang xunku_editor_spider:editor_task_list_url}</th>
			<th class="xw1">{lang xunku_editor_spider:editor_task_list_status}</th>
			<th class="xw1">{lang xunku_editor_spider:editor_task_list_posttime}</th>
			<!-- th class="xw1">{lang xunku_editor_spider:editor_task_list_replaydelay}</th -->
			<th class="xw1">{lang xunku_editor_spider:editor_task_list_operate}</th>
		</tr>
	{loop $tasks $k $t}
		<tr {if $k % 2 == 1} class="alt"{/if}>
			<td id="id_{$k}">{$k}</td>
			<td><a id="url_{$k}" href="{$t}" target="_blank" >{$t}</a></td>
			<td id="status_{$k}" class="status">{lang xunku_editor_spider:editor_task_list_status_text1}</td>
			<td><input type="text" class="px" id="posttime_{$k}" name="posttime" value="{eval echo date('Y-m-d H:i');}" onclick="showcalendar(event, this, true)"></td>
			<!-- td><input type="text" name="replaydelay" size="5" value="1"></td-->
			<td><a href="" onclick="runJob(this);">{lang xunku_editor_spider:editor_task_list_operate_text1}</a>  | <a href onclick="delJob(this);">{lang xunku_editor_spider:editor_task_list_operate_text2}</a>| <a id="readthread_{$k}" href="" target="_blank">{lang xunku_editor_spider:editor_task_list_operate_text3}</a></td>
		</tr>
	{/loop}
	</tbody>
</table>
<div style="float:left">
<button type="button" name="mulitRun" id="btn_mulitRun" class="pn"><em>{lang xunku_editor_spider:editor_task_list_btn_run_job}</em></button>
<button type="button" name="goback" id="btn_goBack" class="pn"><em>{lang xunku_editor_spider:editor_add_return_post}</em></button>
</div>
<div>
<br/>
<br/>
</div>
<script src="source/plugin/xunku_editor_spider/resource/js/jquery.min.js?{VERHASH}"></script>
<script src="source/plugin/xunku_editor_spider/resource/js/jquery.form.js?{VERHASH}"></script>


<script>
var jQ = jQuery.noConflict();

jobvalue= [];//used to save row data.
var fid = '{$fid}';
if (fid) {
	fid = parseInt(fid);
}
var  runallflag = false;
jQ('#btn_mulitRun').bind('click', function() {
	if (runallflag) {
	 alert('you have already run all job before');
	 return true;
	}
	runMulitJob();
	runallflag = true;
});
jQ('#btn_goBack').bind('click', function() {
	window.location.href ='forum.php?mod=post&action=newthread&fid='+fid;	
});

// run job
function runJob(obj) {
	event.returnValue = false;
	event.preventDefault();
	var trobj = jQ(obj).parent().parent();
	trobj.find('.status').html('<p style="color:blue" >{lang xunku_editor_spider:editor_task_list_status_text2}</p>');
	var url = encodeURIComponent(trobj.find('a[target="_blank"]').attr('href'));
	var posttime = encodeURI(jQ.trim(trobj.find('input[name="posttime"]').val()));
	var replaydelay = jQ.trim(trobj.find('input[name="replaydelay"]').val());

	jQ.getJSON('plugin.php?id=xunku_editor_spider:index&action=dotask&fetchUrl=' + url + '&posttime=' + posttime + '&replaydelay=' + replaydelay + '&fid=' + fid, function(data) {
			if (data.statue) {
				if (data.data.tid) {
					tid = data.data.tid;
				} else {
					tid = "";
				}
				var threadurl = 'forum.php?mod=viewthread&tid='+tid;//create viewthread url

				trobj.find('.status').html('<p style="color:green" >{lang xunku_editor_spider:editor_task_list_status_text3}</p>');
				trobj.find('a[id^="readthread"]').attr('href', threadurl);
			} else {
				trobj.find('.status').html('<p style="color:green" >{lang xunku_editor_spider:editor_task_list_status_text4}</p>');
			}
		});
}

//just remove from dom
function delJob(obj) {
	event.returnValue = false;
	event.preventDefault();
	jQ(obj).parent().parent().remove();
}

function ajaxHandle(str) {
	if (jQ.isPlainObject(str)) {
		obj = str;
	} else {
		obj = jQ(str);
	}

	n_index = obj.find('td[id^="id"]').html();
	var id = 'readthread_' + n_index;
	readobj = jQ('#'+id);

	id = 'status_' + n_index;
	statusobj = jQ('#'+id);

	var url = encodeURIComponent(jQ('#url_'+n_index).attr('href'));
	var posttime = encodeURIComponent(jQ.trim(jQ('#posttime_'+n_index).val()));
	//var replaydelay = jQ.trim(obj.find('input[name="replaydelay"]').val());
	statusobj.html('<p style="color:blue" >{lang xunku_editor_spider:editor_task_list_status_text2}</p>');
	var address = 'plugin.php?id=xunku_editor_spider:index&action=dotask';
	address += '&fetchUrl=' + url + '&posttime=' + posttime  +'&fid='+ fid;
	//+ '&replaydelay=' + replaydelay
	
	jQ.getJSON(address, function(data) {
		//CHECK STATUS
		if (data.statue) {
			if (data.data.tid) {
				tid = data.data.tid;
			} else {
				tid = "";
			}
			//create viewthread url
			var threadurl = 'forum.php?mod=viewthread&tid='+tid;
			
			readobj.attr('href', threadurl);
			statusobj.html('<p style="color:green" >{lang xunku_editor_spider:editor_task_list_status_text3}</p>');

		} else {
			statusobj.html('<p style="color:red" >{lang xunku_editor_spider:editor_task_list_status_text4}</p>');
		}
		
		//while jobvalue is not null, get first element .
		if (v = jobvalue.shift()) {
			ajaxHandle(v);
		}
	});
}

function runMulitJob() {
	var joblist = jQ('#joblist tr');
	jQ.each(joblist, function(key, val) {
		if (1 < key) {
			jobvalue.push(val);//save row data into array.
		} else if(1 == key) {
			ajaxHandle(val);//first time , run it right now.
		}
	});

}
//setTimeout('runMulitJob()', '100');
</script>
{subtemplate common/footer}